<template>
  <el-row class="d-flex flex-column" v-if="$route.path === '/caption/resource'">
    <el-col :span="24">
      <form class="el-form query-form el-form--label-right el-form--inline">
        <div class="el-form-item el-form-item--medium" v-if="admin.mType == 2 || admin.mType == 3">
          <label for="name" class="el-form-item__label" style="width: 60px;">区</label>
          <div class="el-form-item__content">
            <div class="el-input el-input--medium">
              <el-select v-model="query.mRegion" placeholder="惠济区" @change="get_townAtt()">
                <el-option label="惠济区" value="1349"></el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="el-form-item el-form-item--medium" v-if="!(admin.mType >= 4)">
          <label for="date" class="el-form-item__label" style="width: 60px;">街道</label>
          <div class="el-form-item__content">
            <el-select
              v-model="query.mZhenid"
              clearable
              placeholder="请选择想要查看的街道"
              @change="get_villageAtt()"
            >
              <el-option
                v-for="(town, index) in townAtt"
                :key="index"
                :label="town.zName"
                :value="town.zhenId"
              ></el-option>
            </el-select>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium" v-if="!(admin.mType == 5)">
          <label for="date" class="el-form-item__label" style="width: 60px;">村</label>
          <div class="el-form-item__content">
            <el-select
              v-model="query.mVillageid"
              clearable
              placeholder="请选择想要查看的村"
              @change="get_aType()"
            >
              <el-option
                v-for="(village, index) in villageAtt"
                :key="index"
                :label="village.vName"
                :value="village.villageId"
              ></el-option>
            </el-select>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium">
          <label for="date" class="el-form-item__label" style="width: 80px;">资源类型</label>
          <div class="el-form-item__content">
            <el-select v-model="query.rType" clearable placeholder="请选择资源类型">
              <el-option
                v-for="(aType, index) in aTypeAtt"
                :key="index"
                :label="aType.R_Type"
                :value="aType.R_Type"
              ></el-option>
            </el-select>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium ml-6">
          <!---->
          <div class="el-form-item__content">
            <el-button type="primary" icon="el-icon-search" @click="to_query">搜索</el-button>
            <!---->
          </div>
        </div>
        <div class="el-form-item el-form-item--medium ml-9" v-if="!(admin.mType == 3)">
          <div class="el-form-item__content">
            <el-button plain icon="el-icon-plus" @click="show_form()">
              <span>添加</span>
            </el-button>
          </div>
        </div>
      </form>
    </el-col>
    <el-col :span="24">
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(225, 225, 225, 0.8)"
        ref="multipleTable"
        :data="items"
        border
        tooltip-effect="dark"
        max-height="600"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="120" align="center"></el-table-column>
        <el-table-column type="index" width="120" align="center"></el-table-column>
        <el-table-column label="资源名称" width="340" align="center" prop="RD_Name"></el-table-column>
        <el-table-column label="位置" width="340" align="center" prop="RD_Adress"></el-table-column>
        <el-table-column label="所属村" width="240" align="center" prop="villName"></el-table-column>
        <el-table-column label="编辑" align="center" width="350" fixed="right">
          <template slot-scope="scope" v-if="$route.path === '/caption/resource'">
            <el-button type="primary" icon="el-icon-tickets" @click="showAll(scope.$index)">详情</el-button>
            <el-button
              v-if="!(admin.mType == 3)"
              type="warning"
              icon="el-icon-edit"
              @click="updata(scope.$index)"
            >编辑</el-button>
            <el-button
              v-if="!(admin.mType == 3)"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.$index, 'RD_Name')"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-row>
          <el-col :span="12">
            <div
              class="grid-content bg-purple d-flex jc-between ai-center"
              v-if="!(admin.mType == 3)"
            >
              <el-button
                type="primary"
                icon="el-icon-delete"
                style="display: block;"
                class="h-1 w-1"
                @click="del_multip('RD_Name')"
              ></el-button>
              <a target='_blank' href="http://www.huitengit.com/excelbiao/ziyuan.zip" class="ml-10 pl-10"><el-button type="text" icon="el-icon-download">模板文件下载</el-button></a>
              <el-upload
                ref="upload"
                :action="$http.defaults.baseURL + 'resources/importResourcesExcel'"
                accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                :on-success="success"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :before-upload="set_form"
                :data="form"
                :auto-upload="false"
                :multiple="true"
                 
              >
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button
                  style="margin-left: 10px;"
                  size="small"
                  type="success"
                  @click="submitUpload"
                >上传到服务器</el-button>
                <div slot="tip" class="el-upload__tip">只能上传excel文件，建议不超过500kb</div>
              </el-upload>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.current_page"
                :page-sizes="pagination.page_sizes"
                :page-size="pagination.page_size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total"
              ></el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-col>
    <el-dialog
      title="新增资源信息"
      v-if="$route.path === '/caption/resource'"
      width="32%"
      :show-close="false"
      :close-on-click-modal="false"
      :lock-scroll="true"
      :visible.sync="dialogFormVisible_create"
    >
      <el-form ref="form" :model="form" label-width="5.625rem" class="d-flex flex-column w-10 p-2">
        <el-col :span="24">
          <el-tabs type="border-card" @tab-click="change_create">
            <el-tab-pane label="资源类型添加">
              <el-col :span="24" class="d-flex flex-column">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">镇</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.mZhenid"
                      clearable
                      placeholder="请选择想要查看的镇"
                      @change="get_villageAtt(true)"
                      :disabled="admin.mType >= 5"
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">村</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.R_Villageid"
                      clearable
                      placeholder="请选择想要查看的村"
                      @change="get_groupAtt(true)"
                      :disabled="admin.mType == 5"
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">组</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.R_zu" clearable placeholder="请选择想要查看的组">
                      <el-option
                        v-for="(group, index) in groupAtt_form"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">资源类型</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.R_type" clearable></el-input>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
            <el-tab-pane label="资源详情添加">
              <el-col :span="24">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">镇</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.mZhenid"
                      clearable
                      placeholder="请选择想要查看的镇"
                      @change="get_villageAtt(true)"
                      :disabled="admin.mType >= 4"
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">村</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.R_Villageid"
                      clearable
                      placeholder="请选择想要查看的村"
                      @change="get_groupAtt(true)"
                      :disabled="admin.mType == 5"
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">组</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.R_zu"
                      clearable
                      placeholder="请选择想要查看的组"
                      @change="get_aType(true)"
                    >
                      <el-option
                        v-for="(group, index) in groupAtt_form"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">资源类型</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.resourcesId" clearable placeholder="请选择资源类型">
                      <el-option
                        v-for="(aType, index) in aTypeAtt_form"
                        :key="index"
                        :label="aType.R_Type"
                        :value="aType.Resources_Id"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">名称</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_Name" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">位置</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_Adress" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">占地面积(平米)</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_AreaCovered" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium d-flex jc-start w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">简介</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-input type="textarea" :rows="4" v-model="form.RD_Details"></el-input>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel_c">取 消</el-button>
        <el-button type="primary" @click="reset_c">重 置</el-button>
        <el-button type="primary" @click="create">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="资源信息编辑"
      v-if="$route.path === '/caption/resource'"
      width="32%"
      :show-close="false"
      :close-on-click-modal="false"
      :lock-scroll="true"
      :visible.sync="dialogFormVisible_edmit"
    >
      <el-form ref="form" :model="form" label-width="5.625rem" class="d-flex flex-column w-10 p-2">
        <el-col :span="24">
          <el-tabs type="border-card" @tab-click="change_create">
            <el-tab-pane label="资源详情编辑">
              <el-col :span="24">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">镇</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.mZhenid"
                      clearable
                      placeholder="请选择想要查看的镇"
                      @change="get_villageAtt(true)"
                      :disabled="admin.mType >= 4"
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">村</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.R_Villageid"
                      clearable
                      placeholder="请选择想要查看的村"
                      @change="get_groupAtt(true)"
                      :disabled="admin.mType == 5"
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">组</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.R_zu" clearable placeholder="请选择想要查看的组">
                      <el-option
                        v-for="(group, index) in groupAtt_form"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                        @change="get_aType(true)"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">资源类型</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.Resources_Id" clearable placeholder="请选择资源类型">
                      <el-option
                        v-for="aType in aTypeAtt_form"
                        :key="aType.R_Type"
                        :label="aType.R_Type"
                        :value="aType.Resources_Id"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">名称</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_Name" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">位置</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_Adress" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">占地面积(平米)</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_AreaCovered" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium d-flex jc-start w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">简介</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-input type="textarea" :rows="4" v-model="form.RD_Details"></el-input>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel_u">取 消</el-button>
        <el-button type="primary" @click="reset_u">重 置</el-button>
        <el-button type="primary" @click="updata_save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="资源信息详情"
      v-if="$route.path === '/caption/resource'"
      width="32%"
      :show-close="true"
      :close-on-click-modal="false"
      :lock-scroll="true"
      :visible.sync="dialogFormVisible_all"
    >
      <el-form ref="form" :model="form" label-width="5.625rem" class="d-flex flex-column w-10 p-2">
        <el-col :span="24">
          <el-tabs type="border-card" @tab-click="change_create">
            <el-tab-pane label="资源详情详情">
              <el-col :span="24">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">镇</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.mZhenid"
                      clearable
                      placeholder="请选择想要查看的镇"
                      @change="get_villageAtt(true)"
                      disabled
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">村</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.R_Villageid"
                      clearable
                      placeholder="请选择想要查看的村"
                      @change="get_groupAtt(true)"
                      disabled
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">组</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.R_zu" disabled clearable placeholder="请选择想要查看的组">
                      <el-option
                        v-for="(group, index) in groupAtt_form"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">资源类型</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.Resources_Id" disabled clearable placeholder="请选择资源类型">
                      <el-option
                        v-for="aType in aTypeAtt_form"
                        :key="aType.R_Type"
                        :label="aType.R_Type"
                        :value="aType.Resources_Id"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">名称</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_Name" disabled clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">位置</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_Adress" disabled clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-center">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">占地面积(平米)</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.RD_AreaCovered" disabled clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium d-flex jc-start w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 6.875rem;">简介</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-input type="textarea" :rows="4" disabled v-model="form.RD_Details"></el-input>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      model: {},
      admin: {},
      obj: {},
      items: [
        //表格内容
      ],
      init_items: [
        //原始表格渲染数据
      ],
      pagination: {
        current_page: 1,
        page_sizes: [20, 30, 50, 100],
        page_size: 20,
        total: 0,
        page_length: 0
      },
      loading: true,
      formLabelWidth: "100px",
      form: {
        managerId: 0
        //弹出框内容
      },
      query: {
        mRegion: "1349"
      },
      //根据村选类型
      region: {},
      //序列id
      index_id: 0,
      index: 0,
      //对应村
      villageAtt: [],
      villageAtt_form: [],
      //对应镇
      townAtt: [],
      townAtt_form: [],
      //对应组
      groupAtt: [],
      groupAtt_form: [],
      //对应类型
      aTypeAtt: [],
      types: [],
      //多删数组
      delAtt: [],
      multipleSelection: [],
      //文件上传
      fileList: [],
      //删除的对象
      del_obj: {},
      delMany: {},
      //模态框
      dialogFormVisible_create: false,
      dialogFormVisible_edmit: false,
      dialogFormVisible_all: false,
      //子分类
      son_types: [],
      //村信息
      villMes: [],
      //类型数组
      aTypeAtt_form: []
    };
  },

  methods: {
    setQuery() {
      if (this.admin.mType == 4) {
        this.query.mRegion = this.admin.mRegion;
        this.query.mZhenid = this.admin.mZhenid;
        this.get_villageAtt();
      }
      if (this.admin.mType == 5) {
        this.query.mRegion = this.admin.mRegion;
        this.query.mZhenid = this.admin.mZhenid;
        this.query.mVillageid = this.admin.mVillageid;
      }
    },
    setObj(obj) {
      obj.mRegion = this.query.mRegion || "1349";
      obj.startPage = this.pagination.current_page;
      obj.pageSize = this.pagination.page_size;
    },
    async get_aType(flag) {
      if (flag) {
        const res = await this.$http.post(
          "/resources/quary11StyleByZuid",
          this.Qs.stringify({
            zuid: this.form.R_zu
          })
        );
        this.aTypeAtt_form = res.data.data;
      } else {
        this.region.forEach(item => {
          if (item.village.villageId == this.query.mVillageid) {
            this.aTypeAtt = item.resourceStyle;
            console.log(item.resourceStyle, "aa");
          }
        });
      }
    },

    async get_townAtt(flag) {
      let res;
      if(!this.query.mRegion) {
        return
      }
      if (flag) {
        res = await this.$http.get(`/caiwu/findzhenByRegionId/1349`);
        this.townAtt_form = res.data.data;
      } else {
        res = await this.$http.get(
          `/caiwu/findzhenByRegionId/${this.query.mRegion}`
        );
        this.townAtt = res.data.data;
      }
    },
    async get_villageAtt(flag) {
      let res;
      if(!this.form.mZhenid && !this.query.mZhenid) {
        return
      }
      if (flag) {
        res = await this.$http.get(
          `/caiwu/findzhenByZhenId/${this.form.mZhenid}`
        );
        this.villageAtt_form = res.data.data;
      } else {
        res = await this.$http.get(
          `/caiwu/findzhenByZhenId/${this.query.mZhenid}`
        );
        this.villageAtt = res.data.data;
        this.query.mVillageid = ''
      }
    },
    async get_groupAtt(flag) {
      let res;
      if(!this.form.R_Villageid && !this.query.mVillageid) {
        return
      }
      if (flag) {
        res = await this.$http.get(`/caiwu/findZu/${this.form.R_Villageid}`);
        this.groupAtt_form = res.data.data;
      } else {
        res = await this.$http.get(`/caiwu/findZu/${this.query.mVillageid}`);
        this.groupAtt = res.data.data;
      }
    },
    //请求数据
    async fetch(obj) {
      this.setObj(obj);
      const res = await this.$http.post(
        "/resources/resourcesDetails",
        this.Qs.stringify(obj)
      );
      if (res.data.data) {
        Array.isArray(res.data.data) && res.data.data.forEach(item => {
          if (item.R_Villageid) {
            item.villName = this.get_villName(item.R_Villageid);
          }
        });
        let { region } = res.data;
        this.region = region;
        this.get_aType();
        this.init_items = res.data;
        this.items = res.data.data;
      } else {
        res.data.data = [];
        this.items = res.data.data;
      }

      this.up_pagination();
      if (
        this.$util.getCookie("current_page") !==
        String(this.pagination.current_page)
      ) {
        this.handleCurrentChange(this.$util.getCookie("current_page"));
      }
      if (res.status == 200) {
        this.loading = false;
      }
    },
    async get_villMes() {
      const res = await this.$http.get("/h/cacheVillage");
      this.villMes = res.data.data;
    },
    get_villName(id) {
      let res;
      this.villMes.forEach(item => {
        if (item.villageId == id) {
          res = item.vName;
        }
      });
      return res;
    },
    handleSelectionChange(val) {
      let att = [];
      this.multipleSelection = val;
      this.multipleSelection.forEach(item => {
        let { ResourcesDetails_Id } = item;
        att.push(ResourcesDetails_Id);
      });
      this.delAtt = att;
    },
    setIndex() {
      this.$basicMes.forEach((item, index) => {
        if (item.name == this.query.town) {
          this.villageAtt = this.$basicMes[index].village;
        }
      });
    },
    get_delObj() {
      let { managerId } = this.admin;
      this.del_obj = { managerId };
    },
    get_delMany() {
      this.delMany = this.delAtt;
    },
    del(index, name) {
      this.get_delObj();
      this.index_id = index;
      this.form = this.items[index];
      this.del_obj.RId = this.form.ResourcesDetails_Id;
      this.$confirm(
        `此操作将永久删除 ${this.items[index][name]}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }
      )
        .then(async index => {
          const res = await this.$http.delete(
            `/resources/resourcesDetailsDelete/${this.form.ResourcesDetails_Id}/${this.admin.managerId}`
          );
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.to_query();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    updata(index, flag) {
      this.index_id = index;
      this.form = this.items[index];
      this.get_townAtt(true);
      this.get_villageAtt(true);
      this.get_groupAtt(true);
      this.get_aType(true);
      if (flag) {
        this.dialogFormVisible_all = true;
      } else {
        this.dialogFormVisible_edmit = true;

         
      }
    },
    async del_multip(name) {
      if (this.delAtt.length == 0) {
        return;
      }
      this.get_delMany();
      let str = "";
      this.multipleSelection.forEach(item => {
        str += item[name] + "\t\t";
      });

      this.$confirm(`此操作将永久删除 ${str}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(async index => {
          const res = await this.$http.delete(
            `/resources/resourcesDetailsDeleteMany/${this.delAtt.toString()}/${
              this.admin.managerId
            }`
          );
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.to_query();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //选择文件
    select_file() {
      let path = this.$route.name;
    },
    //文件上传
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    //分页功能
    up_pagination() {
      this.pagination.page_length = Math.ceil(
        this.init_items.count / this.pagination.page_size
      );
      this.pagination.total = this.init_items.count;
    },
    handleSizeChange(val) {
      this.pagination.page_size = val;
      this.pagination.current_page = 1;
    },
    async handleCurrentChange(val) {
      this.pagination.current_page = val;
      this.$util.setCookie("current_page", this.pagination.current_page);
      this.to_query();
    },
    //query
    to_query() {
      let obj = JSON.parse(JSON.stringify(this.query));
      this.fetch(obj);
    },

    //create
    async change_create(tab) {
      if (tab.label == "资源类型添加") {
        this.get_townAtt(true);
        this.create = this.type_create;
      } else if (tab.label == "资源详情添加") {
        this.get_townAtt(true);
        this.create = this.deta_create;
      }
    },
    async type_create() {
      this.set_form();
      const res = await this.$http.post(
        "/resources/addResources",
        this.Qs.stringify(this.form)
      );
      if (res.status == 200) {
        this.$message({
          type: "success",
          message: `成功啦！！`
        });
        this.to_query();
      }
    },
    async deta_create() {
      this.set_form();
      const res = await this.$http.post(
        "/resources/addResourcesResourcesDetails",
        this.Qs.stringify(this.form)
      );
      if (res.status == 200) {
        this.$message({
          type: "success",
          message: `${res.data.status}`
        });
        this.to_query();
      }
    },
    reset_c() {
      this.$confirm("确定重置新增数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(() => {
        this.$message({
          type: "success",
          message: "已重置"
        });
        this.form = {};
      });
    },
    async show_init() {
      switch (this.admin.mType) {
        case 2:
          this.get_townAtt(true);
          break;
        case 3:
          break;
        case 4:
          await this.get_townAtt(true);
          this.form.mZhenid = this.admin.mZhenid;
          await this.get_villageAtt(true);
          break;
        case 5:
          await this.get_townAtt(true);
          this.form.mZhenid = this.admin.mZhenid;
          await this.get_villageAtt(true);
          this.form.R_Villageid = this.admin.mVillageid;
          this.get_groupAtt(true);
          break;
      }
    },
    show_form() {
      this.clearObj(this.form)
      this.show_init()
      this.get_townAtt(true);
      this.dialogFormVisible_create = true;
    },
    set_form() {
      this.form.managerId = this.admin.managerId;
      for (let item in this.form) {
        if (!this.form[item]) {
          delete this.form[item];
        }
      }
    },
    cancel_c() {
      this.$confirm("取消添加将不会保存该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已取消！"
          });
          this.form = {};
          this.dialogFormVisible_create = false;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    success(res, file, files) {
      if (res.status === "成功") {
        this.$message({
          type: "success",
          message: "上传成功"
        });
      } else {
        this.$message({
          type: "error",
          message: res.status
        });
      }
      this.to_query();
    },
    async create() {
      this.type_create();
    },
    reset_u() {
      this.$confirm("确定重置编辑数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(() => {
        this.$message({
          type: "success",
          message: "已重置"
        });
        for (let item in this.form) {
          if (item !== "_id") {
            this.form[item] = "";
          }
        }
      });
    },
    cancel_u() {
      this.$confirm("取消添加将不会保存该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已取消！"
          });
          setTimeout(() => {
            this.dialogFormVisible_edmit = false;
          }, 200);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    async updata_save() {
      this.form.managerId = this.admin.managerId;
      this.form.hKey = this.form.H_Key;
      this.form.hName = this.form.H_name;
      const res = await this.$http.post(
        "/resources/updateResourcesdetails",
        this.Qs.stringify(this.form)
      );
      if (res.status == 200) {
        this.dialogFormVisible_edmit = false;
        this.to_query();
        this.$message({
          message: "编辑成功",
          type: "success"
        });
      } else {
        this.$message({
          message: "编辑失败",
          type: "error"
        });
      }
    },
    cancel() {
      this.dialogFormVisible_all = false;
    },
    showAll(index) {
      this.updata(index, true);
    }
  },
  created() {
    this.admin = this.$util.handleLocalStorage("get", "admin");
    this.pagination.current_page = 1;
    this.$util.setCookie("current_page", this.pagination.current_page);
    this.get_townAtt();
    this.setQuery();
    let obj = {
      mType: this.admin.mType,
      ...this.query
    };
    this.fetch(obj);
    this.get_villMes();
  }
};
</script>

<style lang="scss" scoped>
.query-form {
  padding-top: 20px;
  margin-bottom: 20px;
  background-color: #f2f2f2;
  display: flex;
  justify-content: flex-start;
}
.el-form--inline .el-form-item {
  display: inline-block;
  margin-right: 10px;
  vertical-align: top;
}
</style>

